// pages/community/community.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBarHeight: app.globalData.navBarHeight,
    menuTopSpace: app.globalData.menuTopSpace,
    menuRightSpace: app.globalData.menuRightSpace,
    menuHeight: app.globalData.menuHeight,
    menuWidth: app.globalData.menuWidth,
    statusBarHeight: app.globalData.statusBarHeight,
    touchx: "",
    touchy: "",
    arr: [],
    openid: "",
    isGiveLike: '',
    nolike: "",
    liked: "",
    giveLikePeople: [],
    commentContent: [],
    textIsShow: false,
    name: "",
    status: "mine",
    mine: "chosed",
    new: ""
  },

  changeMine() {
    let that = this;
    let array = this.data.initarr.filter(item => item["user_id"] === app.globalData.openid);
    let Arr = array.sort((a, b) => {return parseInt(b.time) - parseInt(a.time)});
    this.setData({
      status: "mine",
      mine: "chosed",
      new: ""
    })
    that.setData({
      arr: Arr.map(item => {item.textIsShow = false; return item}),  
      giveLikePeople: Arr.map(item => typeof JSON.parse(item.giveLikeCount) === "string" ?  JSON.parse(JSON.parse(item.giveLikeCount)) : JSON.parse(item.giveLikeCount)),
      commentContent: Arr.map(item => typeof JSON.parse(item.commentContent) === "string" ?  JSON.parse(JSON.parse(item.commentContent)) : JSON.parse(item.commentContent)),
      isGiveLike: [...Arr.map(item => typeof JSON.parse(item.giveLikeCount) === "string" ? JSON.parse(JSON.parse(item.giveLikeCount)).find(item => item == app.globalData.openid) : JSON.parse(item.giveLikeCount).find(item => item == app.globalData.openid))]
    })
  },

  changeNew() {
    let that = this;
    let Arr = this.data.initarr.sort((a, b) => {return parseInt(b.time) - parseInt(a.time)})
    that.setData({
      arr: Arr.map(item => {item.textIsShow = false; return item}),  
      giveLikePeople: Arr.map(item => typeof JSON.parse(item.giveLikeCount) === "string" ?  JSON.parse(JSON.parse(item.giveLikeCount)) : JSON.parse(item.giveLikeCount)),
      commentContent: Arr.map(item => typeof JSON.parse(item.commentContent) === "string" ?  JSON.parse(JSON.parse(item.commentContent)) : JSON.parse(item.commentContent)),
      isGiveLike: [...Arr.map(item => typeof JSON.parse(item.giveLikeCount) === "string" ? JSON.parse(JSON.parse(item.giveLikeCount)).find(item => item == app.globalData.openid) : JSON.parse(item.giveLikeCount).find(item => item == app.globalData.openid))]
    })
    this.setData({
      status: "new",
      mine: "",
      new: "chosed"
    })
  },

  startComment(e) {
    let index = e.currentTarget.dataset.index;
    let str = "arr[" + index + "].textIsShow" 
    this.setData({
      [str]: !this.data.arr[index].textIsShow
    })
  },

  confirmTap(e) {
    console.log(this.data.isGiveLike);
    if(e.detail.value) {
      let index = e.currentTarget.dataset.index;
    let commentarr = this.data.commentContent[index] ? this.data.commentContent[index] : [];
    console.log(commentarr);
    let obj = {};
    obj.name = app.globalData.name;
    obj.content = e.detail.value;
    commentarr.push(obj);
    let str = "commentContent[" + e.currentTarget.dataset.index + "]"
    this.setData({
      [str]: commentarr
    })
    console.log(this.data.commentContent);
    let str2 = "arr[" + e.currentTarget.dataset.index + "].textIsShow"
    this.setData({
      [str2]: false
    })
    wx.request({
      url: 'http://localhost:3000/community/comment',
      method: "post",
      data: {
        commentContent: JSON.stringify(this.data.commentContent[index]),
        time: this.data.arr[e.currentTarget.dataset.index]["time"],
        openid: app.globalData.openid
      },
      success(res) {
        console.log(res);
      }
    })
    }
  },

  length(data) {
    return JSON.parse(data).length
  },

  // 点赞事件
  giveLike(e) {
    console.log(this.data.isGiveLike);
    console.log(this.data.giveLikePeople);
    console.log(this.data.commentContent);
    let index = e.currentTarget.dataset.index;
    console.log(index);
    if(this.data.isGiveLike[index]) {
      let arr = this.data.giveLikePeople[index].filter(item => item != app.globalData.openid)
      let str = "giveLikePeople[" + index + "]";
      let str1 = "isGiveLike[" + e.currentTarget.dataset.index + "]";
      this.setData({
        [str] : arr,
        [str1]: undefined
      })
      this.setData({
        isGiveLike: this.data.isGiveLike
      })
      console.log(this.data.isGiveLike);
      console.log(this.data.arr[e.currentTarget.dataset.index]["time"]);
    } else {
      console.log(this.data.giveLikePeople);
      console.log(e.currentTarget.dataset.index);
      console.log(this.data.giveLikePeople[e.currentTarget.dataset.index]);
      let arr = this.data.giveLikePeople[e.currentTarget.dataset.index];
      arr.push(app.globalData.openid);
      let str = "giveLikePeople[" + e.currentTarget.dataset.index + "]";
      let str1 = "isGiveLike[" + e.currentTarget.dataset.index + "]";
      this.setData({
        [str] : arr,
        [str1] : this.data.arr[e.currentTarget.dataset.index]["user_id"]
      })
      this.setData({
        giveLikePeople: this.data.giveLikePeople
      })
    }

    console.log(this.data.isGiveLike);
    console.log(index);
    console.log(this.data.giveLikePeople);
    wx.request({
      url: 'http://localhost:3000/community/givelike',
      method: "post",
      data: {
        giveLikePeople: JSON.stringify(this.data.giveLikePeople[index]),
        time: this.data.arr[e.currentTarget.dataset.index]["time"],
        openid: app.globalData.openid
      },
      success(res) {
        console.log(res);
      }
    })
  },

  /* 滑动事件 */
  touchStart(e) {
    console.log(e)
    var that = this;
    that.setData({
      touchx: e.changedTouches[0].clientX,
      touchy: e.changedTouches[0].clientY
    })
  },
  touchEnd(e) {
    // console.log(e)
    var that = this;
    let x = e.changedTouches[0].clientX;
    let y = e.changedTouches[0].clientY;
    // console.log(x,y);
    let turn = "";
    console.log(y - that.data.touchy);
    console.log(x - that.data.touchx);
    if (y - that.data.touchy > 10 && Math.abs(x - that.data.touchx) < 50) { //下滑
      turn = "down";
    } else if (y - that.data.touchy < -10 && Math.abs(x - that.data.touchx) < 50) { //上滑
      turn = "up";
    }
    //根据方向进行操作
    if (turn == 'down') {
      //下滑触发操作
      this.setData({
        addClass: "Block"
      })
    } else if (turn == 'up') {
      this.setData({
        addClass: "None"
      })
    }
  },

  addComment() {
    wx.navigateTo({
      url: '../commentPage/commentPage',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      openid: app.globalData.openid,
    });
    let that = this;
    wx.request({
      url: 'http://localhost:3000/community/content',
      method: "post",
      data: {
        name: "tsh"
      },
      success(res) {
        console.log(res.data);
        let array = res.data.filter(item => item["user_id"] === app.globalData.openid);
        let Arr = array.sort((a, b) => {return Number(b.time) - Number(a.time)});
        console.log(Arr);
        that.setData({
          initarr: res.data.map(item => {item.textIsShow = false; return item}),
          arr: Arr.map(item => {item.textIsShow = false; return item}),  
          giveLikePeople: Arr.map(item => typeof JSON.parse(item.giveLikeCount) === "string" ?  JSON.parse(JSON.parse(item.giveLikeCount)) : JSON.parse(item.giveLikeCount)),
          commentContent: Arr.map(item => typeof JSON.parse(item.commentContent) === "string" ?  JSON.parse(JSON.parse(item.commentContent)) : JSON.parse(item.commentContent)),
          isGiveLike: [...Arr.map(item => typeof JSON.parse(item.giveLikeCount) === "string" ? JSON.parse(JSON.parse(item.giveLikeCount)).find(item => item == app.globalData.openid) : JSON.parse(item.giveLikeCount).find(item => item == app.globalData.openid))]
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      openid: app.globalData.openid,
    });
    let that = this;
    wx.request({
      url: 'http://localhost:3000/community/content',
      method: "post",
      data: {
        name: "tsh"
      },
      success(res) {
        console.log(res.data);
        let Arr;
        if(that.data.status === 'mine') {
          let array = res.data.filter(item => item["user_id"] === app.globalData.openid);
          Arr = array.sort((a, b) => {return parseInt(b.time) - parseInt(a.time)});
        } else if(that.data.status === "new") {
          Arr = res.data.sort((a, b) => {return parseInt(b.time) - parseInt(a.time)})
        }
        console.log(Arr)
        that.setData({
          initarr: res.data.map(item => {item.textIsShow = false; return item}),
          arr: Arr.map(item => {item.textIsShow = false; return item}),  
          giveLikePeople: Arr.map(item => typeof JSON.parse(item.giveLikeCount) === "string" ?  JSON.parse(JSON.parse(item.giveLikeCount)) : JSON.parse(item.giveLikeCount)),
          commentContent: Arr.map(item => typeof JSON.parse(item.commentContent) === "string" ?  JSON.parse(JSON.parse(item.commentContent)) : JSON.parse(item.commentContent)),
          isGiveLike: [...Arr.map(item => typeof JSON.parse(item.giveLikeCount) === "string" ? JSON.parse(JSON.parse(item.giveLikeCount)).find(item => item == app.globalData.openid) : JSON.parse(item.giveLikeCount).find(item => item == app.globalData.openid))]
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})